#include "dlg_select_7e_did.h"
#include "ui_dlg_select_7e_did.h"

Dlg_Select_7E_Did::Dlg_Select_7E_Did(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dlg_Select_7E_Did)
{
    ui->setupUi(this);

    this->setWindowFlags(this->windowFlags()| Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
    ui->tableView->setModel(model_7E_did);//sql模型与tableView绑定
    ui->tableView->resizeColumnsToContents();//列宽适应内容
    ui->tableView->setSortingEnabled(true);//排序使能
    ui->tableView->hideColumn(0);//隐藏主键列
    model_7E_did->setSort(1,Qt::AscendingOrder);
    model_7E_did->select();

    for(uint row=0;row<sizeof(radio_find)/sizeof(radio_find[0]);row++)
        for(uint col=0;col<sizeof(radio_find[0])/sizeof(radio_find[0][0]);col++)
        {
            radio_find[row][col] = new Radio_my(ui->groupBox);
            radio_find[row][col]->setText("默认");
            radio_find[row][col]->setGeometry(QRect(20+col*90, 45 + 23*row, 75, 16));
            QObject::connect(radio_find[row][col],SIGNAL(clicked()),
                             this,SLOT(on_radio_find_clicked()));
        }

     ini_save_or_restore(false);
}

Dlg_Select_7E_Did::~Dlg_Select_7E_Did()
{
    ini_save_or_restore(true);
    delete ui;
}
void Dlg_Select_7E_Did::ini_save_or_restore(bool isSave)
{
    QSettings *IniFile = new QSettings("meter.ini", QSettings::IniFormat);
    IniFile->setIniCodec("UTF-8"); ;
    if(isSave == true)
    {

        QString radio_all_str="";
        Radio_my **radio_tmp = &(radio_find[0][0]);
        for(uint idx=0;idx<sizeof(radio_find)/sizeof(radio_find[0][0]);idx++)
        {
            radio_all_str += (*radio_tmp)->text() + " ";
            radio_tmp++;
        }

        IniFile->setValue("radio_find/did_7E", radio_all_str);

        qDebug()<<"7E did radio 搜索 配置信息已保存:"<<radio_all_str;
    }
    else
    {


        QString radio_all_str = IniFile->value("radio_find/did_7E",(QVariant)"").toString();
        QStringList radio_str_L = radio_all_str.split(" ");
        qDebug("7E did 搜索 配置信息共有%d条",radio_str_L.count());

        Radio_my **radio_tmp = &(radio_find[0][0]);
        for(int idx=0;idx<(int)(sizeof(radio_find)/sizeof(radio_find[0][0]));idx++)
        {

            if(idx<radio_str_L.count())
                (*radio_tmp)->setText( radio_str_L.at(idx));
            else
                (*radio_tmp)->setText( "温度");
            radio_tmp++;
        }




        qDebug()<<"7E did radio 搜索 配置信息已读出";
    }

    delete IniFile;

}
void Dlg_Select_7E_Did::on_buttonBox_accepted()
{
    int row = ui->tableView->currentIndex().row();
    QString did_str = model_7E_did->record(row).value("did").toString();//row行，did列，
    emit send_select(did_str);
}

void Dlg_Select_7E_Did::on_pushButtonInsetRow_clicked()
{    
    int rowNum = model_7E_did->rowCount(); //获得表的行数
    model_7E_did->insertRow(rowNum); //添加一行
    model_7E_did->setData(model_7E_did->index(rowNum,1),"");//至少要执行一次写入操作，否则存在空行，会提交失败
    if(model_7E_did->submitAll())
    {
        qDebug()<<"DEBUG添加行成功";
        //QMessageBox::information(this, tr("添加行成功"), "添加行成功");
    }
    else
    {
        qDebug()<<"添加行失败:"<<model_7E_did->lastError().text();
    }
}

void Dlg_Select_7E_Did::on_pushButtonDelRow_clicked()
{
    //QMessageBox::information(this, tr("正在删除"), "正在删除");

    int row = ui->tableView->currentIndex().row();
    qDebug()<<"删除行："<<row;
    model_7E_did->removeRow(row);
    model_7E_did->submitAll();//提交修改
    model_7E_did->setFilter("");//相当于刷新tableView的显示

}

void Dlg_Select_7E_Did::on_pushButtonSave_clicked()
{
    //QMessageBox::information(this, tr("保存"), "保存");
    if(model_7E_did->submitAll())
    {
        qDebug()<<"保存成功";
    }
    else
    {
        qDebug()<<"保存失败:";//<<model_7E_did->lastError().text();
    }
    model_7E_did->setFilter("");//相当于刷新tableView的显示
}
void Dlg_Select_7E_Did::Filter_show(QString SearchStr)
{

    model_7E_did->SetAllFilter(SearchStr);
    model_7E_did->select();
}
void Dlg_Select_7E_Did::on_radio_find_clicked()
{
    Radio_my **radio_tmp = &(radio_find[0][0]);

    for(uint idx=0;idx<sizeof(radio_find)/sizeof(radio_find[0][0]);idx++)
    {
        if((*radio_tmp)->isChecked())
        {
            Filter_show((*radio_tmp)->text());
            return;
        }
        (radio_tmp)++;
    }

}


void Dlg_Select_7E_Did::on_lineEditSearch_cursorPositionChanged(int arg1, int arg2)
{
    arg1 = arg2 + 1;//避免警告
    arg2 =  arg1+ 1;//避免警告
    QString SearchStr = ui->lineEditSearch->text();

//    model_7E_did->setFilter( QString("instruction     LIKE '%%1%' ").arg(SearchStr)
//                           + QString("OR did          LIKE '%%1%' ").arg(SearchStr)
//                           + QString("OR analysisType LIKE '%%1%' ").arg(SearchStr)
//                           + QString("OR data_infor   LIKE '%%1%' ").arg(SearchStr));
    model_7E_did->SetAllFilter(SearchStr);
    model_7E_did->select();

}

void Dlg_Select_7E_Did::on_pushButtonClear_clicked()
{
    ui->lineEditSearch->setText("");
    model_7E_did->setFilter("");//全显
}

void Dlg_Select_7E_Did::on_checkBoxEditMode_stateChanged(int arg1)
{
    if(Qt::Checked == arg1)
    {
        //ui->frame->setEnabled(true);
        ui->pushButtonDelRow->setEnabled(true);
        ui->pushButtonInsetRow->setEnabled(true);
        ui->pushButtonSave->setEnabled(true);
        ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);

    }
    else
    {
        //ui->frame->setEnabled(false);
        ui->pushButtonDelRow->setEnabled(false);
        ui->pushButtonInsetRow->setEnabled(false);
        ui->pushButtonSave->setEnabled(false);
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    }
}


void Dlg_Select_7E_Did::on_tableView_doubleClicked(const QModelIndex &index)
{
    qDebug()<<"双击的行号="<<index.row();
    if(QAbstractItemView::NoEditTriggers == ui->tableView->editTriggers())
    {
        qDebug()<<"选择了did并返回";
        Dlg_Select_7E_Did::on_buttonBox_accepted();
        QDialog::accept();
    }
    else
    {
        qDebug()<<"可编辑单元格";
    }
}


